/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tanks;

/**
 *
 * @author Александр
 */
import com.sun.lwuit.*;
import com.sun.lwuit.Image;
import tanks.Missiles.*;
import tanks.items.*;
import java.io.IOException;

public class Graph {

    public int Resolution = 72;
    private Image canvas;
    int fieldWidth;
    int fieldHeight;
    Game game = Game.Instance();
    Cell[][] Grid;
    //Bitmaps
    Image emptyImg;
    Image destructibleImg;
    Image solidImg;
    //======

    public static int CellSize() {
        return cellSize;
    }
    static int cellSize;

    public Graph(Cell[][] g) {
        cellSize = 30;
        fieldWidth = g.length;
        fieldHeight = g[0].length;
        canvas = Image.createImage(fieldWidth * cellSize, fieldHeight * cellSize);
        Grid = g;
        try {
            emptyImg = Image.createImage("/images/empty.png");
            destructibleImg = Image.createImage("/images/destructible.png");
            solidImg = Image.createImage("/images/solid.png");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        /*
         destructibleB.SetResolution(game.Resolution, game.Resolution);
         solidB.SetResolution(game.Resolution, game.Resolution);
         emptyB.SetResolution(game.Resolution, game.Resolution);
         */
    }

    public Image drawMap() {
        for (int i = 0; i < fieldWidth; i++) {
            for (int j = 0; j < fieldHeight; j++) {
                Graphics gr = canvas.getGraphics();

                switch (Grid[i][j].Block) {
                    case BlockType.Destructible:
                        //gr.FillRectangle(new SolidBrush(Color.Maroon), new RectangleF(i * cellSize, j * cellSize, cellSize,cellSize));
                        gr.drawImage(destructibleImg, i * cellSize, j * cellSize, cellSize, cellSize);
                        break;
                    case BlockType.Solid:
                        //gr.FillRectangle(new SolidBrush(Color.Gray), new RectangleF(i * cellSize, j * cellSize,cellSize,cellSize));
                        gr.drawImage(solidImg, i * cellSize, j * cellSize, cellSize, cellSize);
                        break;
                    case BlockType.Empty:
                        //gr.FillRectangle(new SolidBrush(Color.DarkGreen), new RectangleF(i * cellSize, j * cellSize, cellSize, cellSize));
                        gr.drawImage(emptyImg, i * cellSize, j * cellSize, cellSize, cellSize);

                        if (Grid[i][j].currTank != null) {
                            Grid[i][j].currTank.Draw(canvas);
                        }
                        break;
                }
            }
        }
        for (int i = 0; i < game.Expl.size(); i++) {
            Explosion elem = (Explosion) game.Expl.elementAt(i);
            elem.Draw(canvas);
        }
        for (int i = 0; i < game.Miss.size(); i++) {
            Missile elem = (Missile) game.Miss.elementAt(i);
            elem.Draw(canvas);
        }
        for (int i = 0; i < game.Items.size(); i++) {
            Item elem = (Item) game.Items.elementAt(i);
            elem.Draw(canvas);
        }
        return canvas;
    }
}
